//
//  ESTCustomerViewController.m
//  ESTSCRMAPP
//
//  Created by 李贤立 on 2018/11/2.
//  Copyright © 2018年 Capfer. All rights reserved.
//

#import "ESTCustomerViewController.h"
#import "TYZIndicatorView.h"
#import "ESTPickerTipView.h"

#define Height_SiftView     40
@interface ESTCustomerViewController ()
/**
 *  指示器的数据源
 */
@property(nonatomic, strong) NSArray *indicatorTextArray;
/**
 *  指示器的选择项
 */
@property(nonatomic, assign) NSInteger indicatorNumber;
/**
 *  总共多少个
 */
@property(nonatomic, assign) NSInteger totalNumber;
/**
 *  数量的标签
 */
@property(nonatomic, strong) TYZLabel *totalLabel;
/**
 *  级别的数组
 */
@property(nonatomic, strong) NSArray *rankArray;
/**
 *  选择的级别 默认-1
 */
@property(nonatomic, assign) NSInteger selectedRank;
/**
 *  车系的数组
 */
@property(nonatomic, strong) NSArray *carModeArray;
/**
 *  选择的车系 默认-1
 */
@property(nonatomic, assign) NSInteger selectedCarMode;
@end

@implementation ESTCustomerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupTitleView];
    self.indicatorNumber = 2;
    [self setupIndicatorView];
    self.selectedRank = -1;
    self.selectedCarMode = -1;
    [self setupSiftView];
}

#pragma mark - 设置UI
- (void)setupTitleView {
    TYZView *titleView = [[TYZView alloc] init];
    [self.view addSubview:titleView];
    [titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.view);
        make.height.mas_equalTo(DE_NavigationHeight);
    }];
    titleView.backgroundColor = ThemeColor;
    TYZLabel *titleLabel = [[TYZLabel alloc] init];
    [titleView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(titleView);
        make.bottom.equalTo(titleView).offset(-10);
    }];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = [@"客户" S];
    titleLabel.font = [UIFont boldSystemFontOfSize:CoreInfo.fontSize * 1.2];
    titleLabel.textColor = TYZWhiteColor;
    
    TYZButton *rightButton = [[TYZButton alloc] init];
    [titleView addSubview:rightButton];
    [rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(titleLabel);
        make.right.equalTo(titleView).offset(-20);
    }];
    [rightButton setImage:[UIImage imageNamed:@"na_add"] forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(didClickRightButton) forControlEvents:UIControlEventTouchUpInside];
}
- (void)setupIndicatorView {
    TYZIndicatorModel *model = [[TYZIndicatorModel alloc] init];
    model.font = TYZFont(CoreInfo.fontSize * 0.8);
    model.itemNumber = self.indicatorNumber;
    model.singleton = YES;
    TYZIndicatorView *indicator = [TYZIndicatorView indicatorWithTextArray:self.indicatorTextArray dataModel:model];
    [self.view addSubview:indicator];
    [indicator mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(DE_NavigationHeight);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(Height_Indicator);
    }];
    WEAKSELF
    [indicator setDidSelectedItemBlock:^(NSArray<TYZSelectedModel *> *modelArray) {
        weakSelf.indicatorNumber = modelArray.firstObject.number;
    }];
}
- (void)setupSiftView {
    TYZView *siftView = [[TYZView alloc] init];
    [self.view addSubview:siftView];
    [siftView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view).offset(DE_NavigationHeight + Height_Indicator);
        make.height.mas_equalTo(Height_SiftView);
    }];
    siftView.backgroundColor = TYZWhiteColor;
    [siftView addLine];
    [siftView addLineInTop];
    
    CGFloat width = kWindowWidth / 3;
    CGFloat padding = kWindowWidth / 3;
    ESTPickerTipView *carTypeButton = [[ESTPickerTipView alloc] init];
    carTypeButton.title = [@"按车系" S];
    [siftView addSubview:carTypeButton];
    [carTypeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(siftView);
        make.centerX.equalTo(siftView);
        make.width.mas_equalTo(width);
    }];
    carTypeButton.dataArray = self.carModeArray;
    carTypeButton.textColor = PickerTipViewTextColor_666;
    carTypeButton.titleLabel.font = TYZFont(CoreInfo.fontSize);
    
    ESTPickerTipView *rankButton = [[ESTPickerTipView alloc] init];
    rankButton.title = [@"按级别" S];
    [siftView addSubview:rankButton];
    [rankButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(siftView);
        make.centerX.equalTo(siftView).offset(-padding);
        make.width.mas_equalTo(width);
    }];
    rankButton.dataArray = self.rankArray;
    rankButton.textColor = PickerTipViewTextColor_666;
    rankButton.titleLabel.font = TYZFont(CoreInfo.fontSize);
    
    ESTPickerTipView *moreButton = [[ESTPickerTipView alloc] init];
    moreButton.title = [@"更多筛选" S];
    [siftView addSubview:moreButton];
    [moreButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(siftView);
        make.centerX.equalTo(siftView).offset(padding);
        make.width.mas_equalTo(width);
    }];
    moreButton.textColor = PickerTipViewTextColor_666;
    moreButton.titleLabel.font = TYZFont(CoreInfo.fontSize);
    
    WEAKSELF
    __weak typeof(rankButton) weakRankButton = rankButton;
    __weak typeof(carTypeButton) weakCarButton = carTypeButton;
    [carTypeButton setDidChooseNumberBlock:^(NSArray<NSNumber *> *numberArray) {
        weakSelf.selectedCarMode = [numberArray.lastObject integerValue];
        if (weakSelf.selectedCarMode > 0) {
            weakCarButton.title = weakSelf.carModeArray[weakSelf.selectedCarMode];
            weakCarButton.textColor = PickerTipViewTextColor_Theme;
        } else {
            weakCarButton.title = [@"按车系" S];
            weakCarButton.textColor = PickerTipViewTextColor_666;
        }
        weakCarButton.selectedValue = @[weakSelf.carModeArray[weakSelf.selectedCarMode]];
    }];
    [rankButton setDidChooseNumberBlock:^(NSArray<NSNumber *> *numberArray) {
        weakSelf.selectedRank = [numberArray.lastObject integerValue];
        if (weakSelf.selectedRank > 0) {
            weakRankButton.title = weakSelf.rankArray[weakSelf.selectedRank];
            weakRankButton.textColor = PickerTipViewTextColor_Theme;
        } else {
            weakRankButton.title = [@"按级别" S];
            weakRankButton.textColor = PickerTipViewTextColor_666;
        }
        weakRankButton.selectedValue = @[weakSelf.rankArray[weakSelf.selectedRank]];
    }];
    [moreButton setDidClickCoverBlock:^{

    }];
}
#pragma mark - setter & getter
- (NSArray *)indicatorTextArray {
    if (_indicatorTextArray == nil) {
        _indicatorTextArray = @[
                                [@"线索客户" S],
                                [@"线索战败" S],
                                [@"意向客户" S],
                                [@"订单客户" S],
                                [@"意向战败" S],
                                [@"保有客户" S]
                                ];
    }
    return _indicatorTextArray;
}
- (NSArray *)rankArray {
    if (_rankArray == nil) {
        _rankArray = @[
                       [@"全部" S],
                       [@"H级" S],
                       [@"A级" S],
                       [@"B级" S],
                       [@"C级" S]
                       ];
    }
    return _rankArray;
}
@end
